package com.hemaapp.yjnh.present;

import android.content.Context;
import android.view.View;

import com.hemaapp.yjnh.bean.FarmerCustomOrder;
import com.hemaapp.yjnh.bean.Order;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.listener.OrderView;
import com.hemaapp.yjnh.model.OrderTask;

import xtom.frame.util.XtomToastUtil;

/**
 * 类描述：订单
 * 创建人：GuiyouZhang
 * 创建时间：2016/9/5 13:24
 * 修改备注：
 */
public class OrderPresent {

    Context mContext;
    OrderView mView;
    OrderTask mTask;

    public OrderPresent(Context context, OrderView buyConfirmView, User user) {
        this.mContext = context;
        this.mView = buyConfirmView;
        mTask = new OrderTask(context, user, this);
    }

    public void beforeLoad() {
        mView.beforeLoading();//开始加载网络数据
    }

    public void loadFinish() {
        mView.loadingFinish();
    }

    public void onNetFailed(String content) {
        XtomToastUtil.showShortToast(mContext, content);
    }
    public void getOrderDetail(String orderId) {
        mTask.getOrderDetail(orderId);
    }

    public void setmOrder(Order order) {
        mView.setDatas(order);
    }

    public void operateOrder(String orderId, String keytype, String reason, String description, String shipping_name, String shipping_num) {
        mTask.operateOrder(orderId, keytype, reason, description, shipping_name, shipping_num);
    }

    public void onOrderOperateSuccess(String keytype) {
        mView.onOrderOperateSuccess(keytype);
    }

    /**
     * 底部按钮的顺序依次为：
     * layoutBottom、确认收货、取消订单、代付、支付
     */
    public void setStatusAndBottom(Order mOrder) {
        if (mOrder.getStatetype().equals("2")) {
            mView.setStatus("已删除");
            mView.setBottomButton(new int[]{View.GONE, View.GONE, View.GONE, View.GONE, View.GONE});
            return;
        }
        if (mOrder.getTradetype().equals("0")) {
            mView.setStatus("待付款");
            mView.setBottomButton(new int[]{View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, View.VISIBLE});
        }else if (mOrder.getTradetype().equals("1")) {
            mView.setStatus("待发货");
            mView.setBottomButton(new int[]{View.GONE, View.GONE, View.GONE, View.GONE, View.GONE});
        }else if (mOrder.getTradetype().equals("2")) {
            for (Order.BillChild child : mOrder.getChildren()) {
                if (!child.getItemtype().equals("5")) {//如果没有一件退货完成的商品.那么就显示确认收货
                    mView.setBottomButton(new int[]{View.VISIBLE, View.VISIBLE, View.GONE, View.GONE, View.GONE});
                    mView.setStatus("待收货");
                    return;
                }
            }
            mView.setStatus("问题订单");
            mView.setBottomButton(new int[]{View.GONE, View.GONE, View.GONE, View.GONE, View.GONE});
        }else if (mOrder.getTradetype().equals("3")) {
            mView.setStatus("待评价");
            mView.setBottomButton(new int[]{View.GONE, View.GONE, View.GONE, View.GONE, View.GONE});
        }else if (mOrder.getTradetype().equals("4")) {
            mView.setStatus("交易成功");
            mView.setBottomButton(new int[]{View.GONE, View.GONE, View.GONE, View.GONE, View.GONE});
        }
    }

    /**设置田家的定制订单状态和底部按钮
     * @param mOrder
     */
    public void setStatusAndBottom(FarmerCustomOrder mOrder) {
        if (mOrder.getStatetype().equals("2")) {
            mView.setStatus("已删除");
            mView.setBottomButton(new int[]{View.GONE, View.GONE});
        }
        if (mOrder.getTradetype().equals("0")) {
            mView.setStatus("待付款");
            mView.setBottomButton(new int[]{View.GONE, View.GONE});
        }else if (mOrder.getTradetype().equals("1")) {
            mView.setStatus("待发货");
            mView.setBottomButton(new int[]{View.VISIBLE, View.VISIBLE});
        }else if (mOrder.getTradetype().equals("2")) {
            mView.setStatus("待收货");
            mView.setBottomButton(new int[]{View.GONE, View.GONE});
        }else if (mOrder.getTradetype().equals("3")) {
            mView.setStatus("待评价");
            mView.setBottomButton(new int[]{View.GONE, View.GONE});
        }else if (mOrder.getTradetype().equals("4")) {
            mView.setStatus("交易成功");
            mView.setBottomButton(new int[]{View.GONE, View.GONE});
        }
    }
}
